﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;

using Wicresoft.Shell.Core;
using Wicresoft.Shell.Interfaces;
using Wicresoft.Shell.Logic;

namespace Wicresoft.Shell.Package.AppExp
{

    sealed class Workspace
    {
        /// <summary>
        /// Show workspace form to connect to a specific workspace
        /// </summary>
        /// <returns>Instance of DatabaseLogic</returns>
        public static bool Connect()
        {
            using (ExistConnectForm p = new ExistConnectForm())
            {
                if (DialogResult.OK == p.ShowDialog())
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Connect to a specific workspace by a given connection info
        /// </summary>
        /// <param name="info">a connection info used to connect to workspace</param>
        /// <param name="dlgResult">return DialogResult of 'AddConnectForm', if passwordStr is not set in 'info'.</param>
        /// <returns></returns>
        public static bool Connect(ConnectInfoType info, out DialogResult dlgResult)
        {
            string passwordStr = info.GetPasswordStr();

            dlgResult = DialogResult.None;
            if (null == passwordStr)
            {
                using (AddConnectForm p = new AddConnectForm())
                {
                    p.IsNewConnect = false;
                    p.ServerName = info.ServerName;
                    p.UserName = info.UserName;
                    p.DBName = info.DBName;
                    dlgResult = p.ShowDialog();
                    if (DialogResult.OK == dlgResult)
                    {
                        info.UserName = p.UserName;
                        passwordStr = p.Password;
                        if (p.IsSavedPassword)
                            info.SetPassowrdStr(p.Password);
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            SaveDatabaseLogic(info, passwordStr);

            return true;
        }

        /// <summary>
        /// Connect to a specific workspace by a given connection info
        /// </summary>
        /// <param name="info">a connection info used to connect to workspace</param>
        /// <returns>Instance of DatabaseLogic</returns>
        public static bool Connect(ConnectInfoType info)
        {
            DialogResult dlgResult = DialogResult.None;

            return Connect(info, out dlgResult);
        }

        /// <summary>
        /// Connect to a new workspace
        /// </summary>
        /// <param name="info">connection info of the new workspace</param>
        /// <returns>true if the connect operation successful, otherwise it will return false.</returns>
        public static bool NewConnect(out ConnectInfoType info)
        {
            using (AddConnectForm p = new AddConnectForm())
            {
                p.IsNewConnect = true;
                if (DialogResult.OK == p.ShowDialog())
                {
                    info = new ConnectInfoType();

                    info.ServerName = p.ServerName;
                    info.UserName = p.UserName;
                    info.DBName = p.DBName;
                    if (p.IsSavedPassword)
                        info.SetPassowrdStr(p.Password);

                    SaveDatabaseLogic(info, p.Password);

                    return true;
                }
                else
                {
                    info = null;

                    return false;
                }
            }
        }

        private static void SaveDatabaseLogic(ConnectInfoType info, String pwdStr)
        {
            String cnnStr = String.Empty;
            IDatabaseLogicService service = null;

            cnnStr = DatabaseLogic.BuildDataEntityCnnStr(info.ServerName, info.DBName, info.UserName, pwdStr);
            service = (IDatabaseLogicService)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SDatabaseLogicService));
            service.Instance = new DatabaseLogic(cnnStr);
        }

        public static DatabaseLogic GetDatabaseLogic()
        {
            IDatabaseLogicService service =
                Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SDatabaseLogicService)) as IDatabaseLogicService;
            return service.Instance as DatabaseLogic;
        }
    }
}
